<template>
  <div class="my-container">
    <!-- 已登录头部 -->
    <div class="header user-info" v-if="user">
      <div class="base-info">
        <div class="left">
          <van-image
            class="touxiang"
            fit="cover"
            round
            :src="userInfo.photo"
          />
          <span class="name">{{userInfo.name}}</span>
        </div>
        <div class="right">
          <van-button round size="mini"  to="/user/profile">编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{userInfo.art_count}}</span>
          <span class="text">头条</span>
        </div>
         <div class="data-item">
          <span class="count">{{userInfo.follow_count}}</span>
          <span class="text">关注</span>
        </div>
         <div class="data-item">
          <span class="count">{{userInfo.fans_count}}</span>
          <span class="text">粉丝</span>
        </div>
         <div class="data-item">
          <span class="count">{{userInfo.like_count}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>

    <!-- 未登录头部 -->
    <div class="header not-login" v-else>
      <div class="login-btn" @click="$router.push('/login')">
        <img class="mobile-img" src="~@/assets/mobile.png" alt="">
        <span class="text">登录 / 注册</span>
      </div>
    </div>
    
    <!-- 导航区域  收藏  历史 -->
    <van-grid class="grid-nav" :column-num="2" clickable>
      <van-grid-item class="grid-item">
        <i slot="icon" class="iconfont icon-shoucang"></i>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <i slot="icon" class="iconfont icon-lishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>
    <!-- 单元格 -->
    <van-cell title="消息通知" is-link/>
    <van-cell class="xiaozhi" title="小智同学" is-link/>
    <van-cell class="logout" clickable title="退出登录" v-if="user" @click="logout"/>

  </div>
</template>

<script>
import {mapState} from 'vuex'
import {userInfoAPI} from '@/api'


export default {
  name:'my',
  data(){
    return {
      userInfo:{}
    }
  },
  computed:{
    ...mapState(['user'])
  },
  methods:{
    logout(){
      this.$dialog.confirm({
        title: '确认退出吗?',
      }).then(() => {
          // on confirm
          this.$store.commit('setUser',null)
        })
        .catch(() => {
          // on cancel
        })
    },
    async loadUserInfo(){
      try{
        const { data }=await userInfoAPI()
        this.userInfo=data.data
      }catch(err){
        this.$toast('获取用户数据失败请稍后重试!')
      }

    }
  },
  async created(){
    if(this.user){
      this.loadUserInfo()
    }
  }

}
</script>

<style scoped lang="less">
.my-container{
  .header{
    height: 361px;
    background: url("~@/assets/banner.png");
    background-size: cover;
  }
  .not-login{
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn{
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .mobile-img{
        width: 132px;
        height: 132px;
        margin-bottom: 15px;
      }
      .text{
        font-size: 28px;
        color: #fff;
      }
    }
  }

  .user-info{
    .base-info{
      box-sizing: border-box;
      height: 231px;
      padding: 76px 32px 23px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left{
        display: flex;
        align-items: center;
        .touxiang{
          width: 132px;
          height: 132px;
          margin-right: 23px;
          border: 4px solid #fff;
        }
        .name{
          font-size: 30px;
          color: #fff;
        }

      }
    }
    .data-stats{
      display: flex;
      .data-item{
        flex: 1;
        height: 130px;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        color: #fff;
        .count{
          font-size: 36px;

        }
        .text{
          font-size: 23px;
        }

      }
    }
  }
  .grid-nav{
    .grid-item{
      height: 141px;
      i.iconfont{
        font-size: 45px;
      }
      .icon-shoucang{
        color: #eb5253;
      }
      .icon-lishi{
        color: #ff9d1d;
      }
      span.text{
        font-size: 28px;
      }
    }
  }
  .xiaozhi{
    margin-bottom: 9px;
  }
  .logout{
    text-align: center;
    color: #d86262;
  }

}

</style>